package com.example.healthcondition.mapper;

import com.example.healthcondition.entity.HealthData;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface HealthDataMapper {
    @Select("SELECT * FROM healthdata WHERE user_id = #{userId} ORDER BY recorded_at DESC LIMIT 1")
    HealthData findLatestByUserId(@Param("userId") Integer userId);


    @Insert("INSERT INTO healthdata (user_id, weight, blood_pressure, heart_rate, notes, height, age) " +
            "VALUES (#{userId}, #{weight}, #{bloodPressure}, #{heartRate}, #{notes}, #{height}, #{age})")
    @Options(useGeneratedKeys = true, keyProperty = "dataId")
    int insertHealthData(HealthData healthData);

    @Update("UPDATE healthdata SET weight = #{weight}, blood_pressure = #{bloodPressure}, heart_rate = #{heartRate}, " +
            "notes = #{notes}, height = #{height}, age = #{age} WHERE data_id = #{dataId}")
    int updateHealthData(HealthData healthData);

    @Select("SELECT * FROM healthdata WHERE data_id = #{dataId}")
    HealthData findById(@Param("dataId") Integer dataId);

    @Select("SELECT * FROM healthdata WHERE user_id = #{userId}")
    List<HealthData> findAllByUserId(@Param("userId") Integer userId);
}